﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YJKRevitView;

namespace YJKColumnModify.WallAlignToColumn
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class CmdWallAlignToColumn : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)11;
			if (SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			Selection selection = commandData.Application.ActiveUIDocument.Selection;
			Document document = commandData.Application.ActiveUIDocument.Document;
			Transaction transaction = new Transaction(document, "WallAlignToColumn");
			try
			{
				transaction.Start();
				document.Regenerate();
				Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)4, new ColumnFilter(document), "请选择要对齐的柱边");
				Element element = document.GetElement(reference);
				Face face = element.GetGeometryObjectFromReference(reference) as Face;
				Options options = commandData.Application.Application.Create.NewGeometryOptions();
				options.ComputeReferences = true;
				options.View = commandData.Application.ActiveUIDocument.ActiveView;
				List<GeometryObject> geometryObjectArray = RevitVersionFuncs.GetGeometryObjectArray(element.get_Geometry(options));
				bool flag = false;
				for (int i = 0; i < geometryObjectArray.Count; i++)
				{
					GeometryObject geometryObject = geometryObjectArray[i];
					if (geometryObject is GeometryInstance)
					{
						GeometryInstance geometryInstance = geometryObject as GeometryInstance;
						XYZ xyz = geometryInstance.Transform.OfVector(((PlanarFace)face).FaceNormal);
						XYZ xyz2 = geometryInstance.Transform.OfPoint(((PlanarFace)face).Origin);
						foreach (GeometryObject geometryObject2 in RevitVersionFuncs.GetGeometryObjectArray(geometryInstance.GetInstanceGeometry()))
						{
							Solid solid = geometryObject2 as Solid;
							if (solid.Volume > 0.0)
							{
								foreach (object obj in solid.Faces)
								{
									PlanarFace planarFace = obj as PlanarFace;
									if (!(planarFace == null) && planarFace.FaceNormal.IsAlmostEqualTo(xyz))
									{
										XYZ origin = planarFace.Origin;
										if (xyz2.IsAlmostEqualTo(origin))
										{
											face = planarFace;
											flag = true;
											break;
										}
									}
								}
							}
						}
					}
					if (flag)
					{
						break;
					}
				}
				AlignOperator alignOperator = new AlignOperator(face, document);
				Line faceVerticalBaesZCurve = alignOperator.GetFaceVerticalBaesZCurve(face);
				for (;;)
				{
					try
					{
						reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)4, new WallFilter(document, faceVerticalBaesZCurve), "请选择要对齐的墙边");
						alignOperator.MoveElement(reference);
						continue;
					}
					catch
					{
					}
					break;
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction.HasStarted())
				{
					transaction.RollBack();
				}
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
